๐Ÿ‘ฉโ€๐Ÿ’ป 12985. ์˜ˆ์ƒ ๋Œ€์ง„ํ‘œ

@Seongukbaek Posted August 09, 2022 ยท Updated August 09, 2022 ยท 1 min read

๋ฌธ์ œ

Programmers - ์˜ˆ์ƒ ๋Œ€์ง„ํ‘œ

๐Ÿ“ Logic

while (nA != nB) { nA = (nA + 1) >> 1; nB = (nB + 1) >> 1; answer++; }
  • ์‰ฌํ”„ํŠธ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด ๋‹ค์Œ ๋Œ€์ง„ ๋ฒˆํ˜ธ๋ฅผ ์ •ํ•œ๋‹ค.

๐Ÿ“„ CODE

์ฝ”๋“œ ๋ณด๊ธฐ/์ ‘๊ธฐ๐Ÿ’ซ
class Solution { public int solution(int n, int a, int b) { int answer = 0; int nA = (a + 1) >> 1; int nB = (b + 1) >> 1; while (nA != nB) { nA = (nA + 1) >> 1; nB = (nB + 1) >> 1; answer++; } return answer + 1; } }

โœ๏ธ Review

  • ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค. ๋‹ค์Œ ๋Œ€์ง„ ๋ฒˆํ˜ธ๋ฅผ ์ •ํ•˜๋Š” ๋ฐ˜๋ณต์˜ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ์ฐพ๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฐ ๊ฒŒ ๋ฌธ์ œ์˜€๋‹ค.
@Seongukbaek
๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋Œ€ํ•™์ƒ ๋ฐฑ์„ฑ์šฑ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€